//
//  BaseLabel.m
//  GoldBook
//
//  Created by wmz on 2021/8/15.
//  Copyright © 2021 wmz. All rights reserved.
//

#import "BaseLabel.h"
@interface BaseLabel()

@property (nonatomic, strong) UIView *line;

@end

@implementation BaseLabel

- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines {
    if (self.verticalAlignment) {
        CGRect textRect = [super textRectForBounds:bounds limitedToNumberOfLines:numberOfLines];
        switch (self.verticalAlignment) {
            case VerticalAlignmentTop:
                textRect.origin.y = bounds.origin.y;
                break;
            case VerticalAlignmentBottom:
                textRect.origin.y = bounds.origin.y + bounds.size.height - textRect.size.height;
                break;
            case VerticalAlignmentMiddle:
                // Fall through.
            default:
                textRect.origin.y = bounds.origin.y + (bounds.size.height - textRect.size.height) / 2.0;
        }
        return textRect;
    }
    UIEdgeInsets insets = self.hd_edgeInsets;
    CGRect rect = [super textRectForBounds:UIEdgeInsetsInsetRect(bounds, insets)
                    limitedToNumberOfLines:numberOfLines];

    rect.origin.x -= insets.left;
    rect.origin.y -= insets.top;
    rect.size.width += (insets.left + insets.right);
    rect.size.height += (insets.top + insets.bottom);
    return rect;
}

- (void)drawTextInRect:(CGRect)rect {
    if (self.verticalAlignment) {
        CGRect actualRect = [self textRectForBounds:rect limitedToNumberOfLines:self.numberOfLines];
        [super drawTextInRect:actualRect];
    }else{
        [super drawTextInRect:UIEdgeInsetsInsetRect(rect, self.hd_edgeInsets)];
    }
}


- (void)setVerticalAlignment:(VerticalAlignment)verticalAlignment {
    _verticalAlignment = verticalAlignment;
    [self setNeedsDisplay];
}


- (void)setShowLine:(BOOL)showLine{
    _showLine = showLine;
    self.line.hidden = !showLine;
}

- (void)setLineColor:(UIColor *)lineColor{
    _lineColor = lineColor;
    self.line.backgroundColor = lineColor;
}

- (UIView *)line{
    if (!_line) {
        _line = UIView.new;
        _line.backgroundColor = wColor_line;
        [self addSubview:_line];
        [_line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0.8);
            make.bottom.left.right.mas_equalTo(0);
        }];
    }
    return _line;
}

- (CGFloat)titleWidth{
    if (!_titleWidth) {
        _titleWidth = [self boundingRectWithSize:self.text Font:self.font Size:CGSizeMake(PostWidth * 0.5, PostNum(20))].width;
    }
    return _titleWidth;
}

-(CGSize)boundingRectWithSize:(NSString*)txt Font:(UIFont*) font Size:(CGSize)size{
    
    CGSize _size;
    
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_6_1
    
    NSDictionary *attribute = @{NSFontAttributeName: font};
    
    NSStringDrawingOptions options = NSStringDrawingTruncatesLastVisibleLine |
    
    NSStringDrawingUsesLineFragmentOrigin |
    
    NSStringDrawingUsesFontLeading;
    
    _size = [txt boundingRectWithSize:size options: options attributes:attribute context:nil].size;
    
#else
    
    _size = [txt sizeWithFont:font constrainedToSize:size];
    
#endif
    
    return _size;
    
}

@end
